package com.ibm.mq.test.controller;

import cn.hutool.json.JSONUtil;
import com.ibm.mq.test.dto.TestMessaging;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.UUID;

@RestController
public class ProducerController {
    @Autowired
    private StreamBridge streamBridge;

    @GetMapping("/point")
    public Map<String,String> test(@RequestParam("send") String sendMessage){
        // 构建消息对象
        TestMessaging testMessaging = new TestMessaging()
                .setMsgId(UUID.randomUUID().toString())
                .setMsgText(sendMessage);

        System.out.println("发送的的point msg信息:"+ JSONUtil.toJsonStr(testMessaging));
//        streamBridge.send("demoChannel-out-0", MessageBuilder.withPayload(testMessaging).build());
        streamBridge.send("demoChannel-out-0",JSONUtil.toJsonStr(testMessaging));
        return null;
    }

    @GetMapping("/pub")
    public Map<String,String> pubTest(@RequestParam("send") String sendMessage){
        // 构建消息对象
        TestMessaging testMessaging = new TestMessaging()
                .setMsgId(UUID.randomUUID().toString())
                .setMsgText(sendMessage);

        System.out.println("发送的的pub msg信息:"+ JSONUtil.toJsonStr(testMessaging));
        streamBridge.send("pubSend-out-0", MessageBuilder.withPayload(testMessaging).build());

        return null;
    }
}
